<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

class KomaPostgreSQLDriver extends KomaDatabase implements IKomaDatabaseDriver
{
        function __construct($args = array())
        {
                if(!function_exists('pg_connect')) throw new KomaLibraryException(array('code' => LIBRARY_NOT_INSTALLED, 'class' => __CLASS__, 'library' => 'PostgreSQL'));
                
                $this->createProperties(array(
                              'Host' => isset($args['host']) ? $args['host'] : KOMA_DB_HOST,
			      'Port' => isset($args['port']) ? $args['port'] : 5432,
			      'Timeout' => isset($args['timeout']) ? $args['timeout'] : null,
                              'Options' => isset($args['options']) ? $args['options'] : null,
			      'SSLMode' => isset($args['sslmode']) ? $args['sslmode'] : null,
                              'Username' => isset($args['username']) ? $args['username'] : KOMA_DB_USER,
                              'Password' => isset($args['password']) ? $args['password'] : KOMA_DB_PASS,
                              'Database' => isset($args['database']) ? $args['database'] : KOMA_DB_NAME,
                              'Persistent' => isset($args['persistent']) ? $args['persistent'] : KOMA_DB_PCONNECTION
                              ));

                $this->setReadOnlyProperties(array('Username', 'Password'));
                parent::__construct();
        }

        function __destruct()
        {
                $this->close();
        }
        
        function affectedRows()
        {
                return @pg_affected_rows($this->Result);
        }

	function close()
	{
                if(!$this->Persistent) @pg_close($this->Connection);
	}
        
	function connect()
	{
                $connectionString =     "host='".$this->Host."' ".
                                        "port='".$this->Port."' ".
                                        "dbname='".$this->Database."' ".
                                        "user='".$this->Username."' ".
                                        "password='".$this->Password."'".
                                        ($this->Timeout != null ? " connect_timeout='".$this->Timeout."'" : "").
                                        ($this->Options != null ? " options='".$this->Options."'" : "").
                                        ($this->SSLMode != null ? " sslmode='".$this->SSLMode."'" : "");
                                                         
		
                $this->Connection = ($this->Persistent ? @pg_pconnect($connectionString) : @pg_connect($connectionString));
                
                if(!is_resource($this->Connection))
                {
                        throw new KomaDatabaseException(array('code' => UNABLE_TO_CONNECT,
                                                              'host' => $this->Host));
                }
	}
	
	function fetchArray($result = null)
	{
                $this->assignResult($result);
		return @pg_fetch_array($this->Result, 0, PGSQL_BOTH);
	}

        function fetchAssoc($result = null)
        {
                $this->assignResult($result);
                return @pg_fetch_assoc($this->Result, 0);
        }
        
	function fetchRow($result = null)
	{
                $this->assignResult($result);
		return @pg_fetch_row($this->Result, 0);
	}
	
        function numRows($result  = null)
        {
                $this->assignResult($result);
                return @pg_num_rows($this->Result);
        }
        
	function query($query)
	{
                $this->Result = @pg_query($this->Connection, $query);
                return $this->Result;
	}
        
	static function getInstance($args = array())
	{
                return self::createInstance(__CLASS__, $args);
	}
}
?>